package com.heima.article.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.HotArticleService;
import com.heima.common.cache.CacheService;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.vos.HotArticleVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.InvocationTargetException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class HotArticleServiceImpl implements HotArticleService {

    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private CacheService cacheService;

    @Override
    public void computeHotArticle() {
        LocalDateTime date = LocalDateTime.now().minusDays(5);
        //获得五天内的所有文章
        List<ApArticle> articleByLast5days =  apArticleMapper.findArticleByLast5days(date);
        if (CollectionUtils.isEmpty(articleByLast5days)){
            log.info("五天内没有文章");
            return;
        }

        //进行算分
        List<HotArticleVo> hotArticleVos =  compute(articleByLast5days);

        //要根据频道进行分组
        List<List<HotArticleVo>> arArticleGroupByChannelId = CollectionUtil.groupByField(hotArticleVos, "channelId");
        if (CollectionUtils.isEmpty(arArticleGroupByChannelId)){
            log.warn("分组后没有数据");
            return;
        }
        //将分组后的每一组存入缓存
        for (List<HotArticleVo> apArticles : arArticleGroupByChannelId) {
            if (CollectionUtils.isEmpty(apArticles)){
                continue;
            }
            //进行排序，并存入缓存
            Integer channelId = apArticles.get(0).getChannelId();
            if (channelId == null){
                continue;
            }
            sortedAndChaching(apArticles,channelId.toString());
        }
        //给所有的文章进行排序并存入缓存
        sortedAndChaching(hotArticleVos,ArticleConstants.DEFAULT_TAG);

    }

    private List<HotArticleVo> compute(List<ApArticle> list){
        if (CollectionUtils.isEmpty(list)){
            return new ArrayList<>();
        }
        List<HotArticleVo> hotArticleVos = new ArrayList<>();
        for (ApArticle apArticle : list) {
            Integer comment = apArticle.getComment();
            Integer likes = apArticle.getLikes();
            Integer views = apArticle.getViews();
            Integer collection = apArticle.getCollection();
            Integer score = 0;
            if (views != null) {
                score += views;
            }

            if (likes != null) {
                score += (ArticleConstants.HOT_ARTICLE_LIKE_WEIGHT * likes);
            }

            if (comment != null) {
                score += (ArticleConstants.HOT_ARTICLE_COMMENT_WEIGHT * comment);
            }

            if (collection != null) {
                score += (ArticleConstants.HOT_ARTICLE_COLLECTION_WEIGHT * collection);
            }

            HotArticleVo hotArticleVo = new HotArticleVo();
            hotArticleVo.setScore(score);
            BeanUtils.copyProperties(apArticle,hotArticleVo);
            hotArticleVos.add(hotArticleVo);
        }

        return hotArticleVos;
    }

    private void sortedAndChaching(List<HotArticleVo> hotArticleVos, String channelId) {
        //排序
        List<HotArticleVo> collect = hotArticleVos.stream().sorted((o1, o2) -> o2.getScore() - o1.getScore()).collect(Collectors.toList());
        //缓存
        if (collect.size() > 30){
            collect = collect.subList(0,30);
        }
        cacheService.set(ArticleConstants.HOT_ARTICLE_FIRST_PAGE + channelId, JSON.toJSONString(collect));
    }
}
